/**
************************************************************
* @file         gizwits_product.c
* @brief        Gizwits 控制协议处理,及平台相关的硬件初始化 
* @author       Gizwits
* @date         2016-09-05
* @version      V03010101
* @copyright    Gizwits
* 
* @note         机智云.只为智能硬件而生
*               Gizwits Smart Cloud  for Smart Products
*               链接|增值ֵ|开放|中立|安全|自有|自由|生态
*               www.gizwits.com
*
***********************************************************/
#include "ebox.h"
#include <stdio.h>
#include <string.h>
#include <stm32f10x.h>
#include "gizwits_product.h"
#include "gizwits_protocol.h"
#include "gizwits_userdata.h"
#include "dht11.h"
#include "colorled.h"
#include "lcd_1.8.h"
#include "key_dm.h"





/** 用户区当前设备状态结构体*/
extern dataPoint_t currentDataPoint;
extern Dht11 sensor;//
extern COLORLED led;
extern LCD lcd;
extern COLOR_HSV hsv;
extern COLOR_HSL hsl;
/**@name Gizwits 用户API接口
* @{
*/

/**
* @brief 事件处理接口

* 说明：

* 1.用户可以对WiFi模组状态的变化进行自定义的处理

* 2.用户可以在该函数内添加数据点事件处理逻辑，如调用相关硬件外设的操作接口

* @param[in] info : 事件队列
* @param[in] data : 协议数据
* @param[in] len : 协议数据长度
* @return NULL
* @ref gizwits_protocol.h
*/
int8_t gizwitsEventProcess(eventInfo_t *info, uint8_t *data, uint32_t len)
{
  uint8_t i = 0;
	    int rgbValue = 0;
 //   gizwitsIssued_t *issuedData = (gizwitsIssued_t *)data;
  dataPoint_t *dataPointPtr = (dataPoint_t *)data;
  moduleStatusInfo_t *wifiData = (moduleStatusInfo_t *)data;

  if((NULL == info) || (NULL == data))
  {
    return -1;
  }

  for(i=0; i<info->num; i++)
  {
    switch(info->event[i])
    {

      case EVENT_RGB:
        currentDataPoint.valuergb = dataPointPtr->valuergb;
        GIZWITS_LOG("Evt: EVENT_RGB %d\n", currentDataPoint.valuergb);
        switch(currentDataPoint.valuergb)
        {
          case RGB_VALUE0:
          //user handle
						currentDataPoint.valuergb = (RGB_ENUM_T)LED_R;
	//        ledRgbControl(254,0,0); 
						hsl.h = 0;
						led.color_hsl(hsl);
						GIZWITS_LOG("RED \r\n");
          break;
          case RGB_VALUE1:
            //user handle
						currentDataPoint.valuergb= (RGB_ENUM_T)LED_G; 
						hsl.h = 120;
						led.color_hsl(hsl);	
						GIZWITS_LOG("GREEN \r\n");	
            break;
          case RGB_VALUE2:
            //user handle
						currentDataPoint.valuergb = (RGB_ENUM_T)LED_B; 
						hsl.h = 240;
						led.color_hsl(hsl);	
						GIZWITS_LOG("BLUE \r\n");		
            break;
          default:
            break;
        }
        break;

      case EVENT_RGB_VALUE:
        currentDataPoint.valuergb_value = dataPointPtr->valuergb_value;
        GIZWITS_LOG("Evt:EVENT_RGB_VALUE %d\n",currentDataPoint.valuergb_value);
        //user handle
//			currentDataPoint.valuergb_value= dataPointPtr->valuergb_value;
				rgbValue = dataPointPtr->valuergb_value;//gizX2Y(MOTOR_SPEED_RATIO,MOTOR_SPEED_ADDITION,gizProtocolExchangeBytes(dataPointPtr->valuergb_value));
				hsl.h = rgbValue;
				led.color_hsl(hsl);	
				GIZWITS_LOG("RGB_VALUE %d\r\n",rgbValue);
        break;

      case EVENT_LCD:
        GIZWITS_LOG("Evt: EVENT_LCD\n");
        memcpy((uint8_t *)&currentDataPoint.valuelcd,(uint8_t *)&dataPointPtr->valuelcd,sizeof(currentDataPoint.valuelcd));
        //user handle
	  	for(char i=0;i<20;i++)
			{
					currentDataPoint.valuelcd[i] = dataPointPtr->valuelcd[i];
			}
			GIZWITS_LOG("LCD = [%d] \r\n",dataPointPtr->valuelcd); 
        break;

      case WIFI_SOFTAP:
        break;
      case WIFI_AIRLINK:
        break;
      case WIFI_STATION:
        break;
      case WIFI_CON_ROUTER:
        break;
      case WIFI_DISCON_ROUTER:
        break;
      case WIFI_CON_M2M:
        break;
      case WIFI_DISCON_M2M:
        break;
      case WIFI_RSSI:
        GIZWITS_LOG("RSSI %d\n", wifiData->rssi);
        break;
      case TRANSPARENT_DATA:
        GIZWITS_LOG("TRANSPARENT_DATA \n");
        //user handle , Fetch data from [data] , size is [len]
        break;
      default:
        break;
    }
  }

  return 0;
}

/**@} */
void userHandle(void)
{
    uint8_t ret  = 0;
    int8_t curTem = 0;
    int8_t curHum = 0;
    uint16_t curInfrared = 0;
    static int8_t lastTem = 0;
    static int8_t lastHum = 0;
    static uint32_t irLastTimer = 0;
    static uint32_t thLastTimer = 0;
    
    curInfrared = analog_read(&PB1);//irHandle();  //»ñÈ¡ºìÍâ¶Ô¹ÜÊý¾Ý ¿ª»ò¹Ø
    if(curInfrared != currentDataPoint.valuebright)
    {
        if((gizGetTimerCount() - irLastTimer) > REPORT_TIME_MAX) //timeout = 2S
        {
            currentDataPoint.valuebright = gizX2Y(1,0,gizProtocolExchangeBytes(curInfrared));//Y2X(HUMIDITY_RATIO,HUMIDITY_ADDITION,curInfrared);
            irLastTimer = gizGetTimerCount();
            GIZWITS_LOG("BRIGHT %d %d \r\n", irLastTimer,curInfrared);
        }
    }
    sensor.read();
//		uart1.printf("gizwitsGetTimerCount()-thLastTimer) = %d\%% \r\n",gizwitsGetTimerCount()-thLastTimer);
    if((gizGetTimerCount() - thLastTimer) > REPORT_TIME_MAX) //timeout = 2S
    {
//        ret = dht11Read(&curTem, &curHum);
        
//        if(0 == ret)
//        {
//            if((curTem != lastTem)||(curHum != lastHum))
//            { 
								curTem = sensor.getTemperature();
                curHum = sensor.getHumidity();
                uart1.printf("----------Humidity = %d\%% \r\n", sensor.getHumidity());
                uart1.printf("----------Temperature = %d'C \r\n ", sensor.getTemperature());	
								lcd.printf(0,144,"T %d'C",gizX2Y(TEMPERATURE_RATIO,TEMPERATURE_ADDITION,curTem));
								lcd.printf(60,144,"H %d\%%",gizX2Y(HUMIDITY_RATIO,HUMIDITY_ADDITION,curHum));
                currentDataPoint.valuetemp = gizX2Y(TEMPERATURE_RATIO,TEMPERATURE_ADDITION,curTem);
                currentDataPoint.valuehumi = gizX2Y(HUMIDITY_RATIO,HUMIDITY_ADDITION,curHum);
                
                lastTem = curTem;
                lastHum = curHum;
                
                uart1.printf("Temperature&Humidity  [%d-%d] %d %d\r\n", gizGetTimerCount(), thLastTimer, curTem, curHum);
   //         }
//        }
//        else
//        {
//            uart1.printf("Failed to read DHT11\r\n");
//        }
        
        thLastTimer = gizGetTimerCount();
    }
}


/**
* @brief MCU复位函数

* @param none
* @return none
*/
void mcuRestart(void)
{
    __set_FAULTMASK(1);
    NVIC_SystemReset();
}

/**@} */

/**
* @brief 定时器TIM3中断处理函数

* @param none
* @return none
*/
//void TIMER_IRQ_FUN(void)
//{
//    if (TIM_GetITStatus(TIMER, TIM_IT_Update) != RESET)  
//    {
//        TIM_ClearITPendingBit(TIMER, TIM_IT_Update  );
//        gizTimerMs();
//    }
//}

/**
* @brief 定时器TIM3定时器初始化

* @param none
* @return none
*/
//void timerInit(void)
//{
//    u16 arr = 7199;
//    u16 psc = 9;
//    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
//    NVIC_InitTypeDef NVIC_InitStructure;

//    RCC_APB1PeriphClockCmd(TIMER_RCC, ENABLE); //时钟使能

//    //定时器TIM3初始化
//    TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重载寄存器周期的值
//    TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率的预分频值
//    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
//    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
//    TIM_TimeBaseInit(TIMER, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位0

//    TIM_ITConfig(TIMER, TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断

//    //中断优先级NVIC设置
//    NVIC_InitStructure.NVIC_IRQChannel = TIMER_IRQ;  //TIM3中断
//    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //优占优先级0级
//    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;  //从优先级3级
//    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
//    NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器
//    TIM_Cmd(TIMER, ENABLE);  //使能TIMx
//}

/**
* @brief USART2串口中断函数

* 接收功能，用于接收与WiFi模组间的串口协议数据
* @param none
* @return none
*/
//void UART_IRQ_FUN(void)
//{
//    uint8_t value = 0;
//    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
//    {
//        USART_ClearITPendingBit(USART2,USART_IT_RXNE);
//        value = USART_ReceiveData(USART2);

//        gizPutData(&value, 1);
//    }
//}


/**
* @brief 串口写操作，发送数据到WiFi模组
*
* @param buf      : 数据地址
* @param len       : 数据长度
*
* @return : 正确返回有效数据长度;-1，错误返回
*/
//custom 串口写操作，发送数据到WiFi模组
int8_t uartWrite(uint8_t *buf, uint32_t len)
{
    uint32_t i = 0;
    
    if(NULL == buf)
    {
        return -1;
    }

#ifdef PROTOCOL_DEBUG
    GIZWITS_LOG("MCU2WiFi[%4d:%4d]: ", gizGetTimerCount(), len);
#endif
    
    for(i=0; i<len; i++)
    { 
			 uart2.put_char(buf[i]);
//		 uart2.printf("%02x",buf[i]);
#ifdef PROTOCOL_DEBUG
       GIZWITS_LOG("%02x ", buf[i]);
#endif
    }
    
#ifdef PROTOCOL_DEBUG
    GIZWITS_LOG("\r\n");
#endif
    
    return len;
}

//int32_t uartWrite(uint8_t *buf, uint32_t len)
//{
//    uint32_t i = 0;
//    
//    if(NULL == buf)
//    {
//        return -1;
//    }
//#ifdef PROTOCOL_DEBUG
//    GIZWITS_LOG("MCU2WiFi[%4d:%4d]: ", gizGetTimerCount(), len);
//#endif
//    
//    for(i=0; i<len; i++)
//    {
//        USART_SendData(UART, buf[i]);
//        while (USART_GetFlagStatus(UART, USART_FLAG_TXE) == RESET);//Loop until the end of transmission
//#ifdef PROTOCOL_DEBUG
//        GIZWITS_LOG("%02x ", buf[i]);
//#endif
//        if(i >=2 && buf[i] == 0xFF)
//        {
//          USART_SendData(UART,0x55);
//          while (USART_GetFlagStatus(UART, USART_FLAG_TXE) == RESET);//Loop until the end of transmission
//#ifdef PROTOCOL_DEBUG
//        GIZWITS_LOG("%02x ", 0x55);
//#endif
//        }
//    }
//    
//#ifdef PROTOCOL_DEBUG
//    GIZWITS_LOG("\n");
//#endif
//    
//    return len;
//}

/**
* @brief USART串口初始化

* 用于与WiFi模组间的串口通信
* @param none
* @return none
*/
//void uartInit(void)
//{
//    GPIO_InitTypeDef GPIO_InitStructure;
//    USART_InitTypeDef USART_InitStructure;
//    NVIC_InitTypeDef NVIC_InitStructure;

//    UART_GPIO_Cmd(UART_GPIO_CLK, ENABLE);
//    UART_CLK_Cmd(UART_CLK, ENABLE);
//    UART_AFIO_Cmd(UART_AFIO_CLK, ENABLE);

//    GPIO_InitStructure.GPIO_Pin = UART_TxPin;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_Init(UART_GPIO_PORT, &GPIO_InitStructure);

//    GPIO_InitStructure.GPIO_Pin = UART_RxPin;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//    GPIO_Init(UART_GPIO_PORT, &GPIO_InitStructure);

//    USART_InitStructure.USART_BaudRate = UART_BAUDRATE;
//    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//    USART_InitStructure.USART_StopBits = USART_StopBits_1;
//    USART_InitStructure.USART_Parity = USART_Parity_No;
//    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//    USART_Init(UART, &USART_InitStructure);

//    USART_ITConfig(UART,USART_IT_RXNE,ENABLE);
//    USART_Cmd(UART, ENABLE);
//    USART_ClearFlag(UART, USART_FLAG_TC); /*清空发送完成标志,Transmission Complete flag */

//    /*使能串口中断,并设置优先级*/
//    NVIC_InitStructure.NVIC_IRQChannel = UART_IRQ;
//    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
//    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//    NVIC_Init(&NVIC_InitStructure);
//}

